using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem9 : IProjectEulerProblem
    {
        public IList<Triplet> FindPythagoreanTripletsThatSumTo(int n)
        {
            var list = new List<Triplet>();
            for (int i = 1; i < n; i++)
            {
                if ((i + (i + 1) + (i + 2)) < 1000)
                {
                    for (int j = i + 1; j < n; j++)
                    {
                        var k = (int)Math.Floor(Math.Sqrt((i*i) + (j*j)));
                        var trip = new Triplet(i, j, k);
                        if (trip.Sum == n && trip.IsPythagorean)
                        {
                            list.Add(new Triplet(i, j, k));
                        }
                    }
                }
            }
            return list;
        }

        public int Number
        {
            get { return 9; }
        }

        public string Description
        {
            get { return "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc."; }
        }

        public string Answer
        {
            get
            {
                // this statement asserts that there is only one pythag triple that sums to 1000
                var triplet = FindPythagoreanTripletsThatSumTo(1000).Single();
                return triplet.ToString() + " => " + triplet.Product;
            }
        }
    }

    [TestFixture]
    public class Program9_tests
    {
        [Test]
        public void Can_get_all_pythag_triplets_that_sum_to_a_number()
        {
            var p = new Problem9();
            var list = p.FindPythagoreanTripletsThatSumTo(3);
            Assert.AreEqual(0,list.Count);

            list = p.FindPythagoreanTripletsThatSumTo(12);
            Assert.AreEqual(1, list.Count);
        }
    }
}
